﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO;

namespace HTCCoverImporter
{
    public partial class Form1 : Form
    {
        int index = 0;
        List<string> covers;
        DateTime dtStart;
        List<Exception> lstEx = new List<Exception>();

        public Form1()
        {
            InitializeComponent();
        }

        private void btnSource_Click(object sender, EventArgs e)
        {
            dlgSource.ShowDialog();
            txtSource.Text = dlgSource.SelectedPath;
        }

        private void btnDest_Click(object sender, EventArgs e)
        {
            dlgDest.ShowDialog();
            txtDest.Text = dlgDest.SelectedPath;
        }

        private void btnImport_Click(object sender, EventArgs e)
        {
            CoverImporter importer = new CoverImporter();            

            if (txtSource.Text == "")
            {
                MessageBox.Show("Please select a source path");
                return;
            }

            if (txtDest.Text == "")
            {
                MessageBox.Show("Please select a destination path");
                return;
            }

            importer.Source = txtSource.Text;
            importer.Destination = txtDest.Text;

            string output;
            dtStart = DateTime.Now;
            covers = importer.ImportCovers(out output);
            //txtOutput.Text = output;

            tmrCopy.Enabled = true;
            tmrCopy.Start();

            tmrProg.Enabled = true;
            tmrProg.Start();

            
            //covers[0].Split('\\')[covers[0].Split('\\').Length-2]
        }

        private void tmrCopy_Tick(object sender, EventArgs e)
        {
            try
            {
                if (index == covers.Count - 1)
                {
                    tmrCopy.Stop();
                    tmrCopy.Enabled = false;

                    tmrProg.Stop();
                    tmrProg.Enabled = false;
                    MessageBox.Show("DONE: ");
                    return;
                }
                string dirToCreate = covers[index].Split('\\')[covers[index].Split('\\').Length - 2];

                if (!Directory.Exists(txtDest.Text + "\\" + dirToCreate[0].ToString().ToUpper() + "\\" + dirToCreate))
                    Directory.CreateDirectory(txtDest.Text + "\\" + dirToCreate[0].ToString().ToUpper() + "\\" + dirToCreate);

                File.Copy(covers[index], txtDest.Text + "\\" + dirToCreate[0].ToString().ToUpper() + "\\" + dirToCreate + "\\" + Path.GetFileName(covers[index]));
            }
            catch (Exception ex)
            {
                lstEx.Add(ex);
                txtOutput.Text += "\r\n" + ex.Message;
            }

            index++;
        }

        private void tmrProg_Tick(object sender, EventArgs e)
        {
            //Elapsed Time
            TimeSpan diff = DateTime.Now - dtStart;
            lblTime.Text = ToTime(diff.TotalMilliseconds);

            double progress = (double)index * 100 / covers.Count;
            lblProgress.Text = index + " / " + covers.Count + " (" + Math.Round(progress,2) + " %)";
            progressBar1.Value = (int)progress;            
            Application.DoEvents();
        }

        private string ToTime(double msecs)
        {
            int secs = (int)msecs / 1000;
            int hours;
            int mins;

            hours = (int)secs / 3600;
            mins = (int)secs % 3600 / 60;
            secs = (int)secs % 3600 % 60;

            return string.Format("{0:00}:{1:00}:{2:00}",hours, mins, secs);
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            //System.Drawing.Image img = System.Drawing.Image.FromFile(@"C:\Users\ZEF\Projects\HiddenTreasureComics\HTComicsWeb\Images\Covers\B\Batman\276.jpg");
            //System.Drawing.Image img2 = System.Drawing.Image.FromFile(@"C:\Users\ZEF\Projects\HiddenTreasureComics\HTComicsWeb\Images\Covers\B\Batman\277.jpg");

            List<string> files = new List<string>();
            string []dirs = System.IO.Directory.GetDirectories(@"C:\Users\ZEF\Projects\HiddenTreasureComics\HTComicsWeb\Images\Covers\");
            for (int i = 0; i <= dirs.Length - 1; i++)
            {
                FormatFinder.OddOneOut(dirs[i], files);
            }            
        }
    }
}
